﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Input;
using System.Globalization;
using StateMachine;

namespace LilTmSim
{
	//public class StateNodeBefore : Panel, INode, INotifyPropertyChanged
	//{
	//     public delegate void LocationChangedDelegate(StateNode sender);
	//     public event LocationChangedDelegate OnDragEnd;

	//     public delegate void OneStateNode(StateNode sender, bool value);
	//     public event OneStateNode OnIsInitialChanged;
		

	//     public event PropertyChangedEventHandler PropertyChanged;

	//     private static int _uid = 0;
	//     private static int uid { get { return _uid++; } }
	//     private static int getUid { get { return _uid; } }

	//     /// <summary>
	//     /// Gets or sets the Canvas.Left position of this element
	//     /// </summary>
	//     public double X 
	//     { 
	//          get
	//          {
	//               return Canvas.GetLeft(this);
	//          } 
	//          set
	//          {
	//               if (value != X)
	//               {
	//                    this.OldLocation = new Point(X, Y);
	//                    Canvas.SetLeft(this, value);
	//                    if (PropertyChanged != null)
	//                    {
	//                         PropertyChanged(this, new PropertyChangedEventArgs("X"));
	//                         PropertyChanged(this, new PropertyChangedEventArgs("Location"));
	//                    }
	//                    this.StateData.Location = new Point(X, Y);
	//               }
	//          } 
	//     }
		
	//     /// <summary>
	//     /// Gets or sets the Canvas.Top position of this element
	//     /// </summary>
	//     public double Y
	//     {
	//          get
	//          {
	//               return Canvas.GetTop(this);
	//          }
	//          set
	//          {
	//               if (value != Y)
	//               {
	//                    this.OldLocation = new Point(X, Y);
	//                    Canvas.SetTop(this, value);
	//                    if (PropertyChanged != null)
	//                    {
	//                         PropertyChanged(this, new PropertyChangedEventArgs("Y"));
	//                         PropertyChanged(this, new PropertyChangedEventArgs("Location"));
	//                    }
	//                    this.StateData.Location = new Point(X, Y);
	//               }
	//          }
	//     }

	//     /// <summary>
	//     /// Text is a Dependency Property.
	//     /// </summary>
	//     public string Text
	//     {
	//          get { return (string)GetValue(TextProperty); }
	//          set { SetValue(TextProperty, value); }
	//     }
	//     public static readonly DependencyProperty TextProperty =
	//          DependencyProperty.Register("Text", typeof(string), typeof(StateNode), new UIPropertyMetadata("", new PropertyChangedCallback(TextChanged)));

	//     static void TextChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = (o as StateNode);
	//          RedrawNode(n);
	//          n.StateData.Name = n.Text;
	//     }

	//     /// <summary>
	//     /// StateData is a Dependency Property.
	//     /// </summary>
	//     public State StateData
	//     {
	//          get { return (State)GetValue(StateDataProperty); }
	//          set { SetValue(StateDataProperty, value); }
	//     }
	//     public static readonly DependencyProperty StateDataProperty =
	//          DependencyProperty.Register("StateData", typeof(State), typeof(StateNode), new UIPropertyMetadata(null));


	//     /// <summary>
	//     /// TransitionsLeaving is a Dependency Property.
	//     /// </summary>
	//     public TransitionNodeCollection TransitionsLeaving
	//     {
	//          get { return (TransitionNodeCollection)GetValue(TransitionsLeavingProperty); }
	//          set { SetValue(TransitionsLeavingProperty, value); }
	//     }
	//     public static readonly DependencyProperty TransitionsLeavingProperty =
	//          DependencyProperty.Register("TransitionsLeaving", typeof(TransitionNodeCollection), typeof(StateNode), new UIPropertyMetadata(null, new PropertyChangedCallback(TransitionsLeavingChanged)));

	//     static void TransitionsLeavingChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = o as StateNode;
	//          n.StateData.Transitions = new TransitionCollection(n.TransitionsLeaving.TransitionDataList);
	//     }


	//     /// <summary>
	//     /// TransitionsEntering is a Dependency Property.
	//     /// </summary>
	//     public TransitionNodeCollection TransitionsEntering
	//     {
	//          get { return (TransitionNodeCollection)GetValue(TransitionsEnteringProperty); }
	//          set { SetValue(TransitionsEnteringProperty, value); }
	//     }
	//     public static readonly DependencyProperty TransitionsEnteringProperty =
	//          DependencyProperty.Register("TransitionsEntering", typeof(TransitionNodeCollection), typeof(StateNode), new UIPropertyMetadata(null));


	//     /// <summary>
	//     /// Label is a Dependency Property.
	//     /// </summary>
	//     public string Label
	//     {
	//          get { return (string)GetValue(LabelProperty); }
	//          set { SetValue(LabelProperty, value); }
	//     }
	//     public static readonly DependencyProperty LabelProperty =
	//          DependencyProperty.Register("Label", typeof(string), typeof(StateNode), new UIPropertyMetadata("", new PropertyChangedCallback(LabelChanged)));

	//     static void LabelChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = o as StateNode;
	//          RedrawNode(n);
	//          n.StateData.Label = n.Label;
	//     }

	//     public Point Location
	//     {
	//          get { return new Point(X, Y); }
	//          set 
	//          {
	//               if (Location != value)
	//               {
	//                    //this.OldLocation = new Point(X, Y);
	//                    X = value.X;
	//                    Y = value.Y;

	//                    if (PropertyChanged != null)
	//                    {
	//                         PropertyChanged(this, new PropertyChangedEventArgs("Location"));
	//                    }
	//                    StateData.Location = Location;
	//               }
	//          }
	//     }

	//     /// <summary>
	//     /// Gets the unique id of this node. Id is a Dependency Property.
	//     /// </summary>
	//     public int Id
	//     {
	//          get { return (int)GetValue(IdProperty); }
	//          set { SetValue(IdProperty, value); }
	//     }
	//     public static readonly DependencyProperty IdProperty =
	//          DependencyProperty.Register("Id", typeof(int), typeof(StateNode), new UIPropertyMetadata(-1, new PropertyChangedCallback(IdChanged)));

	//     static void IdChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = o as StateNode;
	//          n.StateData.Id = n.Id.ToString();
	//     }


	//     /// <summary>
	//     /// IsMouseHovering is a Dependency Property.
	//     /// </summary>
	//     public bool IsMouseHovering
	//     {
	//          get { return (bool)GetValue(IsMouseHoveringProperty); }
	//          set { SetValue(IsMouseHoveringProperty, value); }
	//     }
	//     public static readonly DependencyProperty IsMouseHoveringProperty =
	//          DependencyProperty.Register("IsMouseHovering", typeof(bool), typeof(StateNode), new UIPropertyMetadata(false, new PropertyChangedCallback(IsMouseHoveringChanged)));

	//     static void IsMouseHoveringChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          RedrawNode(o as StateNode);
	//     }

	//     /// <summary>
	//     /// IsSelected is a Dependency Property.
	//     /// </summary>
	//     public bool IsSelected
	//     {
	//          get { return (bool)GetValue(IsSelectedProperty); }
	//          set { SetValue(IsSelectedProperty, value); }
	//     }
	//     public static readonly DependencyProperty IsSelectedProperty =
	//          DependencyProperty.Register("IsSelected", typeof(bool), typeof(StateNode), new UIPropertyMetadata(false, new PropertyChangedCallback(IsSelectedChanged)));

	//     static void IsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          RedrawNode(o as StateNode);
	//     }

	//     #region Visual Properties


	//     /// <summary>
	//     /// MouseHoverBorder is a Dependency Property.
	//     /// </summary>
	//     public Brush MouseHoverBorder
	//     {
	//          get { return (Brush)GetValue(MouseHoverBorderProperty); }
	//          set { SetValue(MouseHoverBorderProperty, value); }
	//     }
	//     public static readonly DependencyProperty MouseHoverBorderProperty =
	//          DependencyProperty.Register("MouseHoverBorder", typeof(Brush), typeof(StateNode), new UIPropertyMetadata(Brushes.Green));

	//     /// <summary>
	//     /// MouseHoverBackground is a Dependency Property.
	//     /// </summary>
	//     public Brush MouseHoverBackground
	//     {
	//          get { return (Brush)GetValue(MouseHoverBackgroundProperty); }
	//          set { SetValue(MouseHoverBackgroundProperty, value); }
	//     }
	//     public static readonly DependencyProperty MouseHoverBackgroundProperty =
	//          DependencyProperty.Register("MouseHoverBackground", typeof(Brush), typeof(StateNode), new UIPropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 210, 255, 230))));

	//     /// <summary>
	//     /// DefaultBorderColor is a Dependency Property.
	//     /// </summary>
	//     public Brush DefaultBorderColor
	//     {
	//          get { return (Brush)GetValue(DefaultBorderColorProperty); }
	//          set { SetValue(DefaultBorderColorProperty, value); }
	//     }
	//     public static readonly DependencyProperty DefaultBorderColorProperty =
	//          DependencyProperty.Register("DefaultBorderColor", typeof(Brush), typeof(StateNode), new UIPropertyMetadata(Brushes.Blue));
		
	//     /// <summary>
	//     /// DefaultBackgroundColor is a Dependency Property.
	//     /// </summary>
	//     public Brush DefaultBackgroundColor
	//     {
	//          get { return (Brush)GetValue(DefaultBackgroundColorProperty); }
	//          set { SetValue(DefaultBackgroundColorProperty, value); }
	//     }
	//     public static readonly DependencyProperty DefaultBackgroundColorProperty =
	//          DependencyProperty.Register("DefaultBackgroundColor", typeof(Brush), typeof(StateNode), new UIPropertyMetadata(Brushes.AliceBlue));

	//     /// <summary>
	//     /// BorderThickness is a Dependency Property.
	//     /// </summary>
	//     public double BorderThickness
	//     {
	//          get { return (double)GetValue(BorderThicknessProperty); }
	//          set { SetValue(BorderThicknessProperty, value); }
	//     }
	//     public static readonly DependencyProperty BorderThicknessProperty =
	//          DependencyProperty.Register("BorderThickness", typeof(double), typeof(StateNode), new UIPropertyMetadata(3.0));

	//     private static void RedrawNode(StateNode node)
	//     {
	//          node.InvalidateVisual();
	//     }

	//     /// <summary>
	//     /// HoverBorderThickness is a Dependency Property.
	//     /// </summary>
	//     public double HoverBorderThickness
	//     {
	//          get { return (double)GetValue(HoverBorderThicknessProperty); }
	//          set { SetValue(HoverBorderThicknessProperty, value); }
	//     }
	//     public static readonly DependencyProperty HoverBorderThicknessProperty =
	//          DependencyProperty.Register("HoverBorderThickness", typeof(double), typeof(StateNode), new UIPropertyMetadata(3.0));


	//     /// <summary>
	//     /// IsFinal is a Dependency Property.
	//     /// </summary>
	//     public bool IsFinal
	//     {
	//          get { return (bool)GetValue(IsFinalProperty); }
	//          set { SetValue(IsFinalProperty, value); }
	//     }
	//     public static readonly DependencyProperty IsFinalProperty =
	//          DependencyProperty.Register("IsFinal", typeof(bool), typeof(StateNode), new UIPropertyMetadata(false, new PropertyChangedCallback(IsFinalChanged)));

	//     static void IsFinalChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = o as StateNode;
	//          RedrawNode(n);
	//          n.StateData.IsFinal = n.IsFinal;
	//     }

	//     /// <summary>
	//     /// IsInitial is a Dependency Property.
	//     /// </summary>
	//     public bool IsInitial
	//     {
	//          get { return (bool)GetValue(IsInitialProperty); }
	//          set { SetValue(IsInitialProperty, value); }
	//     }
	//     public static readonly DependencyProperty IsInitialProperty =
	//          DependencyProperty.Register("IsInitial", typeof(bool), typeof(StateNode), new UIPropertyMetadata(false, new PropertyChangedCallback(IsInitialChanged)));

	//     static void IsInitialChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          StateNode n = (o as StateNode);
	//          RedrawNode(n);
	//          if (n.OnIsInitialChanged != null)
	//          {
	//               n.OnIsInitialChanged(o as StateNode, n.IsInitial);
	//          }

	//          n.StateData.IsInitial = n.IsInitial;
	//     }

	//     /// <summary>
	//     /// EndStateScale is a Dependency Property.
	//     /// </summary>
	//     public double EndStateScale
	//     {
	//          get { return (double)GetValue(EndStateScaleProperty); }
	//          set { SetValue(EndStateScaleProperty, value); }
	//     }
	//     public static readonly DependencyProperty EndStateScaleProperty =
	//          DependencyProperty.Register("EndStateScale", typeof(double), typeof(StateNode), new UIPropertyMetadata(0.4, new PropertyChangedCallback(EndStateScaleChanged)));

	//     static void EndStateScaleChanged(DependencyObject o, DependencyPropertyChangedEventArgs a)
	//     {
	//          if ((o as StateNode).IsFinal) RedrawNode(o as StateNode);
	//     }

	//     /// <summary>
	//     /// OldLocation is a Dependency Property.
	//     /// </summary>
	//     public Point OldLocation
	//     {
	//          get { return (Point)GetValue(OldLocationProperty); }
	//          set { SetValue(OldLocationProperty, value); }
	//     }
	//     public static readonly DependencyProperty OldLocationProperty =
	//          DependencyProperty.Register("OldLocation", typeof(Point), typeof(StateNode), new UIPropertyMetadata(null));

		
	//     #endregion
	
		

	//     public StateNodeCollection Container { get; set; }

	//     public StateNode(State stateData) : this(uid, stateData) { }
	//     public StateNode() : this(uid, new State(getUid.ToString())){}
	//     public StateNode(int id, State stateData)
	//     {
	//          StateData = stateData;
	//          this.Id = id;
	//          this.Text = "q" + id.ToString();
	//          this.Width = 50;
	//          this.Height = 50;
	//          this.MouseEnter += new MouseEventHandler(StateNode_MouseEnter);
	//          Mouse.AddMouseDownHandler(this, StateNode_DragStart);

	//          TransitionsEntering = new TransitionNodeCollection();
	//          TransitionsLeaving = new TransitionNodeCollection();

	//          InitializeContextMenu();

	//          this.IsFinal = this.IsFinal;
	//          this.IsInitial = this.IsInitial;
	//     }

	//     //MenuItem initialMenu;
	//     void InitializeContextMenu()
	//     {
	//          ContextMenu menu = new ContextMenu();
	//          this.ContextMenu = menu;

	//          AddMenuItem("Change Text", delegate { ChangeText(); });
	//          AddMenuItem("Change Label", delegate { ChangeLabel(); });
	//          menu.Items.Add(new Separator());
	//          MenuItem initialMenu = AddMenuItem("Initial", delegate { IsInitial = !IsInitial; }, true);
	//          AddMenuItem("Final", delegate { IsFinal = !IsFinal; }, true);
	//          menu.Items.Add(new Separator());
	//          AddMenuItem("Delete", delegate { Container.Remove(this); });

	//          menu.Opened += delegate { initialMenu.IsChecked = this.IsInitial; };

	//          this.MouseLeftButtonDown += new MouseButtonEventHandler(StateNode_MouseLeftButtonDown);
	//     }

	//     void StateNode_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	//     {
	//          if (Mouse.DirectlyOver == this as IInputElement && e.ClickCount == 2)
	//          {
	//               ChangeText();
				
	//          }
	//     }

	//     private void ChangeText()
	//     {
	//          this.Text = this.Id.ToString();
	//     }

	//     private void ChangeLabel()
	//     {
	//          MessageBox.Show("Change Label Not Implemented");
	//     }


	//     MenuItem AddMenuItem(string title, RoutedEventHandler onClick) { return AddMenuItem(title, onClick, false); }
	//     MenuItem AddMenuItem(string title, RoutedEventHandler onClick, bool check)
	//     {
	//          MenuItem item = new MenuItem();
	//          item.Header = title;
	//          item.IsCheckable = check;
	//          item.Click += onClick;
	//          this.ContextMenu.Items.Add(item);

	//          return item;
	//     }

	//     Point mouseOffset;
	//     void StateNode_DragStart(object sender, MouseEventArgs e)
	//     {
	//          if (!Keyboard.IsKeyDown(Key.LeftCtrl))
	//          {
	//               mouseOffset = e.GetPosition(this as IInputElement);
	//               Mouse.AddMouseMoveHandler(this, StateNode_DragMove);
	//               Mouse.AddMouseUpHandler(this, StateNode_DragEnd);
	//               this.CaptureMouse();
	//          }
	//     }

	//     void StateNode_DragMove(object sender, MouseEventArgs e)
	//     {
	//          Point pos = Mouse.GetPosition((IInputElement)Parent);
	//          this.X = pos.X - mouseOffset.X;
	//          this.Y = pos.Y - mouseOffset.Y;
	//     }

	//     void StateNode_DragEnd(object sender, MouseEventArgs e)
	//     {
	//          this.ReleaseMouseCapture();
	//          Mouse.RemoveMouseMoveHandler(this, StateNode_DragMove);
	//          Mouse.RemoveMouseUpHandler(this, StateNode_DragEnd);

	//          if(OnDragEnd != null)
	//               OnDragEnd(this);
	//     }

	//     void StateNode_MouseEnter(object sender, MouseEventArgs e)
	//     {
	//          this.IsMouseHovering = true;
	//          this.MouseLeave += new MouseEventHandler(StateNode_MouseLeave);
	//     }

	//     void StateNode_MouseLeave(object sender, MouseEventArgs e)
	//     {
	//          this.IsMouseHovering = false;
	//          this.MouseLeave -= new MouseEventHandler(StateNode_MouseLeave);
	//     }

	//     protected override void OnRender(System.Windows.Media.DrawingContext dc)
	//     {
	//          base.OnRender(dc);

	//          Brush border = DefaultBorderColor;
	//          Brush background = DefaultBackgroundColor;
	//          double thickness = BorderThickness;

	//          if (IsSelected | IsMouseOver)
	//          {
	//               border = MouseHoverBorder;
	//               background = MouseHoverBackground;
	//               thickness = HoverBorderThickness;
	//          }

	//          dc.DrawEllipse(background, new Pen(border, thickness), new Point(0, 0), this.Width / 2, this.Height / 2);

	//          if (IsFinal)
	//          {
	//               dc.DrawEllipse(background, new Pen(border, thickness), new Point(0, 0), this.Width * EndStateScale, this.Height * EndStateScale);
	//          }

	//          if (IsInitial)
	//          {
	//               Pen arrow = new Pen(border, thickness);
	//               Point p1 = new Point(-25, 0);
	//               Point p2 = new Point(-35, -10);
	//               Point p3 = new Point(-35, 10);
	//               Point p4 = new Point(-45, 0);
	//               dc.DrawLine(arrow, p1, p2);
	//               dc.DrawLine(arrow, p1, p3);
	//               dc.DrawLine(arrow, p1, p4);
	//          }

	//          FormattedText ft = FormattedText;
	//          dc.DrawText(ft, new Point(-(ft.Width / 2), -(ft.Height / 2)));			
	//     }

	//     public override string ToString()
	//     {
	//          return Text;
	//     }

	//     private FormattedText FormattedText
	//     {
	//          get
	//          {
	//               FormattedText formattedText = new FormattedText(
	//               this.Text,
	//               CultureInfo.GetCultureInfo("en-us"),
	//               FlowDirection.LeftToRight,
	//               new Typeface("Verdana"),
	//               12,
	//               Brushes.Black);
	//               formattedText.SetFontWeight(FontWeights.Bold);

	//               return formattedText;
	//          }
	//     }
	//}
}
